﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Kooboo.IoC;

namespace Kooboo.Commerce.Payments.PayPal
{
    internal static class Services
    {
        internal static Context Current
        {
            get
            {
                return ContextContainer.Current.Resolve<Context>();
            }
        }

        internal static Models.Settings FindSettings()
        {
            var settings = Current.DataConext.Payment_Paypals
                .Select(i => new Models.Settings
                {
                    APIPassword = i.APIPassword,
                    APISignature = i.APISignature,
                    APIUsername = i.APIUsername,
                    MerchantAccount = i.MerchantAccount,
                    GoLive = i.GoLive
                })
                .FirstOrDefault();

            return settings;
        }

        internal static bool Update(Models.Settings settings)
        {
            var dataContext = Current.DataConext;
            Payment_Paypal find = dataContext.Payment_Paypals
                .FirstOrDefault();

            if (find == null)
            {
                find = new Payment_Paypal();
                dataContext.Payment_Paypals.InsertOnSubmit(find);
            }

            find.APIPassword = settings.APIPassword;
            find.APISignature = settings.APISignature;
            find.APIUsername = settings.APIUsername;
            find.MerchantAccount = settings.MerchantAccount;
            find.GoLive = settings.GoLive;
            dataContext.SubmitChanges();

            return true;

        }

    }
}